<%@ page session="true" buffer="16kb" import="java.sql.*,java.util.*,java.text.*"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%
response.setDateHeader("Expires", 0); // date in the past
response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // HTTP/1.1 
response.addHeader("Cache-Control", "post-check=0, pre-check=0"); 
response.addHeader("Pragma", "no-cache"); // HTTP/1.0 
%>
<% Locale locale = Locale.getDefault();
locale = new Locale("zh","CN"); 
response.setLocale(locale);%>
<% session.setMaxInactiveInterval(30*60); %>
<% 
String login = (String) session.getAttribute("project1_status");
if (login == null || !login.equals("login")) {
response.sendRedirect("login.jsp");
response.flushBuffer(); 
return; 
}%>
<%int ewAllowAdmin = 16; 
int ewCurSec = 31;%>
<%@ include file="db.jsp" %>
<%@ include file="jspmkrfn.jsp" %>
<%
String tmpfld = null;
String escapeString = "\\\\'";
request.setCharacterEncoding("utf-8");

// Get action
String a = request.getParameter("a");
String key = "";
if (a == null || a.length() == 0) {
	key = request.getParameter("key");
	if (key != null && key.length() > 0) {
		a = "C"; // Copy record
	} else {
		a = "I"; // Display blank record
	}
}
Object x_PRODUCTION_NUM = null;
Object x_PRODUCTION_NAME = null;
Object x_PRICE = null;
Object x_COMPANY_NUM = null;
Object x_SALE_TIME = null;

// Open Connection to the database
try{
	Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
	ResultSet rs = null;
	if (a.equals("C")){ // Get a record to display
		String tkey = "'" + key.replaceAll("'",escapeString) + "'";
		String strsql = "SELECT * FROM `production_info` WHERE `PRODUCTION_NUM`=" + tkey;
		rs = stmt.executeQuery(strsql);
		if (!rs.next()){
			rs.close();
			rs = null;
			stmt.close();
			stmt = null;
			conn.close();
			conn = null;
			out.clear();
			response.sendRedirect("production_infolist.jsp");
			response.flushBuffer();
			return;
		}
		rs.first();

			// Get the field contents
	if (rs.getString("PRODUCTION_NUM") != null){
		x_PRODUCTION_NUM = rs.getString("PRODUCTION_NUM");
	}else{
		x_PRODUCTION_NUM = "";
	}
	if (rs.getString("PRODUCTION_NAME") != null){
		x_PRODUCTION_NAME = rs.getString("PRODUCTION_NAME");
	}else{
		x_PRODUCTION_NAME = "";
	}
	x_PRICE = String.valueOf(rs.getDouble("PRICE"));
	if (rs.getString("COMPANY_NUM") != null){
		x_COMPANY_NUM = rs.getString("COMPANY_NUM");
	}else{
		x_COMPANY_NUM = "";
	}
	if (rs.getTimestamp("SALE_TIME") != null){
		x_SALE_TIME = rs.getTimestamp("SALE_TIME");
	}else{
		x_SALE_TIME = null;
	}
		rs.close();
		rs = null;
	}else if (a.equals("A")) { // Add

		// Get fields from form
		if (request.getParameter("x_PRODUCTION_NUM") != null){
			x_PRODUCTION_NUM = (String) request.getParameter("x_PRODUCTION_NUM");
		}else{
			x_PRODUCTION_NUM = "";
		}
		if (request.getParameter("x_PRODUCTION_NAME") != null){
			x_PRODUCTION_NAME = (String) request.getParameter("x_PRODUCTION_NAME");
		}else{
			x_PRODUCTION_NAME = "";
		}
		if (request.getParameter("x_PRICE") != null){
			x_PRICE = (String) request.getParameter("x_PRICE");
		}else{
			x_PRICE = "";
		}
		if (request.getParameter("x_COMPANY_NUM") != null){
			x_COMPANY_NUM = (String) request.getParameter("x_COMPANY_NUM");
		}else{
			x_COMPANY_NUM = "";
		}
		if (request.getParameter("x_SALE_TIME") != null){
			x_SALE_TIME = (String) request.getParameter("x_SALE_TIME");
		}else{
			x_SALE_TIME = "";
		}

		// Open record
		String strsql = "SELECT * FROM `production_info` WHERE 0 = 1";
		rs = stmt.executeQuery(strsql);
		rs.moveToInsertRow();

		// Field PRODUCTION_NUM
		tmpfld = ((String) x_PRODUCTION_NUM);
		if (tmpfld == null || tmpfld.trim().length() == 0) {
			tmpfld = "";
		}
		if (tmpfld == null) {
			rs.updateNull("PRODUCTION_NUM");
		}else{
		String srchfld = "'" + tmpfld + "'";
			srchfld = srchfld.replaceAll("'","\\\\'");
			strsql = "SELECT * FROM `production_info` WHERE `PRODUCTION_NUM` = '" + srchfld +"'";
			Statement stmtchk = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			ResultSet rschk = stmtchk.executeQuery(strsql);
			if (rschk.next()) {
				out.print("Duplicate key for PRODUCTION_NUM, value = " + tmpfld + "<br>");
				out.print("Press [Previous Page] key to continue!");
				return;
			}
			rschk.close();
			rschk = null;
			rs.updateString("PRODUCTION_NUM", tmpfld);
		}

		// Field PRODUCTION_NAME
		tmpfld = ((String) x_PRODUCTION_NAME);
		if (tmpfld == null || tmpfld.trim().length() == 0) {
			tmpfld = "";
		}
		if (tmpfld == null) {
			rs.updateNull("PRODUCTION_NAME");
		}else{
			rs.updateString("PRODUCTION_NAME", tmpfld);
		}

		// Field PRICE
		tmpfld = ((String) x_PRICE).trim();
		if (!IsNumeric(tmpfld)) {tmpfld = "0";}
		if (tmpfld != null) {
			rs.updateDouble("PRICE", Double.parseDouble(tmpfld));
		} else {
			rs.updateNull("PRICE");
		}

		// Field COMPANY_NUM
		tmpfld = ((String) x_COMPANY_NUM);
		if (tmpfld == null || tmpfld.trim().length() == 0) {
			tmpfld = null;
		}
		if (tmpfld == null) {
			rs.updateNull("COMPANY_NUM");
		}else{
			rs.updateString("COMPANY_NUM", tmpfld);
		}

		// Field SALE_TIME
//		if (IsDate((String) x_SALE_TIME,"DATE", locale)) {
//			rs.updateTimestamp("SALE_TIME", EW_UnFormatDateTime((String)x_SALE_TIME,"DATE", locale));
//		}else{
//			rs.updateNull("SALE_TIME");
//		}
		
		rs.insertRow();
		rs.close();
		rs = null;
		stmt.close();
		stmt = null;
		conn.close();
		conn = null;
		out.clear();
		response.sendRedirect("production_infolist.jsp");
		response.flushBuffer();
		return;
	}
}catch (SQLException ex){
	out.println(ex.toString());
}
%>
<%@ include file="header.jsp" %>
<div class="content">
	<div class="search">
		<p><span class="jspmaker table_name">账号信息添加<br></span></p>
		<a class="jspmaker back" href="production_infolist.jsp">返回</a>
	</div>
	<script language="JavaScript" src="ew.js"></script>
	<script language="JavaScript">
	<!-- start Javascript
	function  EW_checkMyForm(EW_this) {
	if (EW_this.x_PRODUCTION_NUM && !EW_hasValue(EW_this.x_PRODUCTION_NUM, "TEXT" )) {
	            if (!EW_onError(EW_this, EW_this.x_PRODUCTION_NUM, "TEXT", "Invalid Field - PRODUCTION NUM"))
	                return false; 
	        }
	if (EW_this.x_PRODUCTION_NAME && !EW_hasValue(EW_this.x_PRODUCTION_NAME, "TEXT" )) {
	            if (!EW_onError(EW_this, EW_this.x_PRODUCTION_NAME, "TEXT", "Invalid Field - PRODUCTION NAME"))
	                return false; 
	        }
	if (EW_this.x_PRICE && !EW_hasValue(EW_this.x_PRICE, "TEXT" )) {
	            if (!EW_onError(EW_this, EW_this.x_PRICE, "TEXT", "Incorrect floating point number - PRICE"))
	                return false; 
	        }
	if (EW_this.x_PRICE && !EW_checknumber(EW_this.x_PRICE.value)) {
	        if (!EW_onError(EW_this, EW_this.x_PRICE, "TEXT", "Incorrect floating point number - PRICE"))
	            return false; 
	        }
	return true;
	}
	
	// end JavaScript -->
	</script>
	<form onSubmit="return EW_checkMyForm(this);"  action="production_infoadd.jsp" method="post">
	<p>
	<input type="hidden" name="a" value="A">
	<table border="0" cellspacing="1" cellpadding="4" bgcolor="#CCCCCC">
		<tr>
			<td bgcolor="#594FBF"><span class="jspmaker" style="color: #FFFFFF;">商品编号</span>&nbsp;</td>
			<td bgcolor="#F5F5F5"><span class="jspmaker"><input type="text" name="x_PRODUCTION_NUM" size="30" maxlength="20" value="<%= HTMLEncode((String)x_PRODUCTION_NUM) %>"></span>&nbsp;</td>
		</tr>
		<tr>
			<td bgcolor="#594FBF"><span class="jspmaker" style="color: #FFFFFF;">商品名称</span>&nbsp;</td>
			<td bgcolor="#F5F5F5"><span class="jspmaker"><input type="text" name="x_PRODUCTION_NAME" size="30" maxlength="40" value="<%= HTMLEncode((String)x_PRODUCTION_NAME) %>"></span>&nbsp;</td>
		</tr>
		<tr>
			<td bgcolor="#594FBF"><span class="jspmaker" style="color: #FFFFFF;">价格</span>&nbsp;</td>
			<td bgcolor="#F5F5F5"><span class="jspmaker"><input type="text" name="x_PRICE" size="30" value="<%= HTMLEncode((String)x_PRICE) %>"></span>&nbsp;</td>
		</tr>
		<tr>
			<td bgcolor="#594FBF"><span class="jspmaker" style="color: #FFFFFF;">企业编号</span>&nbsp;</td>
			<td bgcolor="#F5F5F5"><span class="jspmaker"><input type="text" name="x_COMPANY_NUM" size="30" maxlength="20" value="<%= HTMLEncode((String)x_COMPANY_NUM) %>"></span>&nbsp;</td>
		</tr>
	</table>
	<p>
	<input class="editbutton" type="submit" name="Action" value="提交">
	</form>
</div>
<%@ include file="footer.jsp" %>
